(function() {
	var $op = $('div.op');
	var $scope = $('div.scope');
	var $dialog = $('#add-items-dialog');
	
	$dialog.dialog({
		autoOpen: false,
		modal: true,
		width: 800,
		height: 600,
		open: function(event, ui) {
			//todo
		},
		close: function(event, ui){
			$("table.items", $dialog).data('selection', []);
			$("table.items input.selector", $dialog).removeAttr('checked').trigger('change');
		}
	});
	
	$('button.add-item', $scope).button().click(function(){
		$dialog.dialog('option', 'buttons', {
			确定: function(){
				var $selectedRows = $('tr.row-selected', $dialog);
				$selectedRows.each(function(){
					var $row = $(this);
					var numIid = $row.attr('num_iid');
					if (!$('div.selected-items tr[num_iid="' + numIid + '"]', $scope).length > 0)
					{
						var $img = $('td.main-pic img', $row);
						var picUrl = $img.attr('src');
						var title = $img.attr('title');
						var $item = $('<tr><td><img/></td><td class="title"></td><td><a href="#" class="delete">删除</a></td></tr>');
						$item.attr('num_iid', numIid);
						$('img', $item).attr('src', picUrl);
						$('td.title', $item).text(title);
						$item.appendTo('div.selected-items table', $scope);
						$('a.delete', $item).click(function(){
							$item.remove();
							return false;
						});
					}
				});
				$dialog.dialog('close');
			},
			取消: function(){
				$dialog.dialog('close');
			}
		});
		$dialog.dialog('open');
		return false;
	});
	
	$('button.clear-items', $scope).button().click(function(){
		$('div.selected-items table', $scope).empty();
	});
	
	$('input[name="scope"]', $scope).change(function(){
		var scope = $(this).val();
		if (scope == 2)
		{
			$('div.categories-selector').slideDown('slow');
		}
		else
		{
			$('div.categories-selector').hide();
		}
		if (scope == 3)
		{
			$('div.items-selector', $scope).slideDown('slow');
		}
		else
		{
			$('div.items-selector', $scope).hide();
		}
	});
	
	$('input[name="op"]', $op).change(function(){
		$('td.label', $op).removeClass('strong');
		var $tr = $(this).closest('tr');
		$('td.label', $tr).addClass('strong');
	});
	
	$('div.categories-selector input:checkbox', $scope).change(function(){
		var cid = $(this).parent().attr('value');
		var pid = $(this).parent().attr('parent');
		if (parseInt(pid) == 0)
		{
			var v = $(this).attr('checked');
			if (v)
			{
				$('div[parent="' + cid + '"] input').attr('checked', 'checked');
			}
			else
			{
				$('div[parent="' + cid + '"] input').removeAttr('checked');
			}
		}
	});
	
	$('button.batch-update').button().click(function(){
		var $_this = $(this);
		$('input:text').removeClass('err');
		var op = $('input:checked[name="op"]').val();
		var data = {};
		if (op == 1)
		{
			var $from = $('input[name="from"]');
			var from = $from.val();
			if (!from)
			{
				$from.addClass('err');
				$from.attr('title', '必填项，不能放空。');
				$from.focus();
				return false;
			}
			var to = $('input[name="to"]').val();
			data['replacement.from'] = from;
			data['replacement.to'] = to;
		}
		else if (op == 2)
		{
			var deleteKeyword = $('input[name="delete-keyword"]').val();
			data['replacement.from'] = deleteKeyword;
			data['replacement.to'] = '';
		}
		else if (op == 3)
		{
			var prefix = $('input[name="prefix"]').val();
			data['prefix.prefix'] = prefix;
		}
		else if (op == 4)
		{
			var suffix = $('input[name="suffix"]').val();
			data['suffix.suffix'] = suffix;
		}
		
		var scopeType = $('input[name="scope"]:checked', $scope).val();
		data['scope'] = scopeType;
		var cids = [];
		$("div[name='sellerCids'] input:checked", $scope).each(function(){
			cids.push($(this).parent().attr('value'));
		});
		data['sellerCids'] = cids.join();
		var items = [];
		$('div.selected-items tr', $scope).each(function(){
			var numIid = $(this).attr('num_iid');
			if (numIid)
			{
				items.push(numIid);
			}
		});
		data['numIids'] = items.join(',');
		$(this).button('disable');
		var $msg = $('span.msg', $(this).parent());
		$msg.html('<img src="images/loading-small.gif"/>正在执行，请稍候...').show();
		$.ajax({
			url: 'batch-update-title',
			data: data,
			type: 'POST',
			success: function(){
				$msg.text('已完成。').delay(5000).fadeOut('slow');;
				$_this.button('enable');
			}
		});
		return false;
	});
})();